<?php
/**
 * Created by PhpStorm.
 * User: 仙女大人
 * Date: 2019/5/31
 * Time: 15:36
 */

namespace app\admin\controller;
use think\Controller;
use think\Request;
use think\Db;
use think\Session;
use app\admin\model\Jquery as JqueryModel;
use app\admin\model\City as CityModel;

class Jquery extends Base
{
    public function jlist()
    {
        // 分页输出列表 每页显示10条数据
        $list = JqueryModel::paginate(10);
        $this->assign('list',$list);

        $data = Db::name('city')->where('parent_id',1)->select();
        $this->assign('data',$data);

        if(request()->isAjax()){

            $code = input('post.code');
            $lists = db('city')->where('parent_id', $code)->select();

            return json(['code' => 1, 'data' => $lists, 'msg' => 'ok']);
        }

        return $this->fetch();
    }

    // 省
    public function region(){
        $model = new JqueryModel();
        $region = input("post.code");
        $province = $model->region($region);
        if($province)
        {
            return json(['code' => 1, 'data' => $province, 'msg' => 'ok']);
        }else
        {
            return json(['code' => 2, 'msg' => '无数据']);
        }

    }
    // 市
    public function city(){
        $model = new JqueryModel();
        $region = input("post.code");
        $city = $model->region($region);
        if($city)
        {
            return json(['code' => 1, 'data' => $city, 'msg' => 'ok']);
        }else
        {
            return json(['code' => 2, 'msg' => '无数据']);
        }
    }

    // 区
    public function area(){
        $model = new JqueryModel();
        $region = input("post.code");
        $area = $model->region($region);
        if($area)
        {
            return json(['code' => 1, 'data' => $area, 'msg' => 'ok']);
        }else
        {
            return json(['code' => 2, 'msg' => '无数据']);
        }
    }


    // 渲染城市
    public function nav()
    {
        $data = Db::name('city')->select();
        $new_data = getTree($data,0);
        $this->assign('data',$new_data);
        return $this->fetch();
    }

    // 渲染添加
    public function add()
    {
        return $this->fetch();
    }

    public function insert()
    {
        $request = Request::instance();
        $data = $request->post();
        $goods = new JqueryModel();
        $res =  $goods->save($data);

        if ($res) {
            $this->success('新增成功', 'jlist');
        } else {
            $this->error('新增失败','add');
        }
    }

    // 删除操作
    public function del()
    {
        $request = Request::instance();
        $id = $request->get('id');
        $res = JqueryModel::destroy($id);
        if($res)
        {
            $this->success("删除成功,正在跳转~~~","jlist");
        }else
        {
            $this->error('删除失败，返回列表页','jlist');
        }
    }

    //修改页面
    public function upd()
    {
        $id = input('get.');
        $user = JqueryModel::find($id);
        return view('upd',['res' =>$user]);
    }

    //修改数据
    public function update()
    {
        $id = $_POST['id'];
        $request = Request::instance();
        $data = $request->post();
        // var_dump($data);die;
        $goods = new JqueryModel();
        if ($goods->update(['id'=>$id])->save($data)) {
            $this->success('修改成功', 'jlist');
        } else {
            $this->error('修改失败','upd');
        }
    }

    // 即点即改
    public function save()
    {
        $id = Request::instance()->param('text_id');//ID值
        $fd = Request::instance()->param('fd');    //字段名字
        $val = Request::instance()->param('val');//值
        $data[$fd] = $val;
        Db::name('jquery')->where('id', $id)->update($data);
    }

    // 批量删除
    public function delAll(){
        $data = input('post.');
        $id = $data['ids'];
        $res = JqueryModel::destroy($id);
        if($res)
        {
            return json(['code' => 1, 'data' => $res, 'msg' => 'ok']);
        }else
        {
            return json(['code' => 2, 'msg' => '批删失败']);
        }
    }

    public function Treeview()
    {
        $data1 = Db::name('city')->select();
        $data = getTree($data1,0);
        // halt($data);
        $this->assign('data',$data);

        return view();
    }
}